var dg;
$(function() {
	dg = $('#log_list');
	var total = 0;
    var pageSize = 20;
    var pageNum = 1;
	dg.datagrid({
		title:'运维日志列表',
		url:contextPath+'/console/userLog/listByPageForEasyui?pageNo=1&pageSize='+pageSize,
		loadMsg: '加载中...',
		width: '100%',
        height: $("#log_list_div").height(),
        fitColumns:true,
		pagination:true,
        pageSize:pageSize,
        pageList:[5,10,20,40,80],
        singleSelect:true,
        selectOnCheck:false,
        checkOnSelect:false,
        rownumbers:true,
        //toolbar: [],
        columns:[[
			{field: 'remake', title: '操作说明',width: 150, halign:'center',align:'center'},
			{field: 'params', title: '入参', width: 600,halign:'center',align:'center'},
			{field: 'userName', title: '操作人', width: 200,halign:'center',align:'center'},
			{field: 'ip', title: '操作IP', width: 200,halign:'center',align:'center'},
			{field: 'createTime', title: '操作时间', width: 350,halign:'center',align:'center'}
        ]],
        onLoadSuccess:function (data) {
            total = dg.datagrid('getData').total;
            var pager = dg.datagrid('getPager');
            pager.pagination({
                onSelectPage: function (pageNumber, size) {
					var remake = $("#remark").val();
					var userName = $("#operator").combobox("getValue");
					var ip = $('#opip').val();
					var startTime = $('#startTime').datebox("getValue");
					var endTime = $('#endTime').datebox("getValue");
					pageSize = size;
                    pageNum = pageNumber == 0 ?1:pageNumber;
                    var url = contextPath+'/console/userLog/listByPageForEasyui?pageSize='+size+'&pageNo='+pageNum;
                    $.get(
                    	url,
                    	 {
							 remake:remake,
							 userName:userName,
							 ip: ip,
							 startTime: startTime,
							 endTime: endTime
                    	 },
                        function (data) {
                            dg.datagrid('loadData', data);
                        },
                        'json'
                    );

                }
            });
        },
        onClickRow : function(index, rowData) {
        	Ajax({
        		url: contextPath+"/console/userLog/detailView/"+rowData.id,
        		isMask : true,
        		success: function (data) {
        			if (data.success) {
        				var app = data.data;
        				for (var key in app) {
        					if($("#" + key).hasClass("json")) {
        						$("#" + key).html(JsonUti.convertToStringHtml(jQuery.parseJSON(app[key])));
        					}else {
        						$("#" + key).html(app[key]);
        					}
            			}
        				$("#add-dlg").dialog("open");
        			}else {
        				apexLayer.Error(data.note);
        			}
        			
        		}
        	});
        }
    });

	$("#add-dlg").dialog({
		title:'详细信息',
		width: 800,
		height: 400,
		closed: true,
		cache: false,
		modal: true,
		shadow: false,
		onClose: function () {
		},
		buttons: [{
			text: '确定',
			iconCls:'icon-ok',
			handler: function () {
				$("#add-dlg").dialog('close');
			}
		}]
	});
});

$(function(){
	$("#look").click(function(data) {
		searchByCondition();
	});
	$("#reset").click(function(){
		doReset();
	});
	$("#operator").combobox({
		method: 'get',
		url: contextPath + '/console/user/getAllUsers',
		valueField: 'id',
		textField: 'name',
		editable: false,
		panelHeight:　'auto',
		//:　'----请选择----',
		onLoadSuccess: function (data) {

		},
		onLoadError: function () {

		},
		onShowPanel: function () {
			$(this).combobox('panel').height();

		},
		onChange: function (newValue,oldValue) {
		}
	});

});

function doReset() {
	// {} 作用是清除之前的查询条件
	dg.datagrid('load', {});
	$('#remark').textbox('clear');
	$('#operator').combobox('reset');
	$('#opip').textbox('clear');
	$('#startTime').datebox('reset');
	$('#endTime').datebox('reset');
}

function searchByCondition(num,size) {
	var remake = $("#remark").val();
	var userName = $("#operator").combobox("getValue");
	var ip = $('#opip').val();
	var startTime = $('#startTime').datebox("getValue");
	var endTime = $('#endTime').datebox("getValue");
	var pageNum = num==null||num==undefined?1:num;
	var pageSize = size==null||size==undefined?20:size;
	var url =contextPath+'/console/userLog/listByPageForEasyui?pageSize='+pageSize+'&pageNo=1';
	$.get(
		url,
		{
			remake:remake,
			userName:userName,
			ip: ip,
			startTime: startTime,
			endTime: endTime,
			pageNum:pageNum,
			pageSize:pageSize
		},
		function (data) {
			//console.log(data.rows);
			//console.log(data.total);
			dg.datagrid('loadData', data);
		},
		'json'
	);
}